﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Medical.Business;
using Medical.Entity;

/// <summary>
/// 新增答疑
/// </summary>
public partial class webadmin_DoFeedBack : AdminPage
{
    /// <summary>
    /// 操作类型
    /// </summary>
    protected string strType = string.Empty;

    /// <summary>
    /// ID
    /// </summary>
    protected int nID;

    protected void Page_Load(object sender, EventArgs e)
    {
        strType = Common.GetQueryValue("type", 0);
        if (!Page.IsPostBack)
        {
            InitSort();
            
        }
        if (strType == "edit")
        {
            nID = Convert.ToInt32(Common.GetQueryValue("id", 0));
            if (!Page.IsPostBack)
            {
                InitData();
            }
        }
    }

    /// <summary>
    /// 权限控制
    /// </summary>
    /// <returns></returns>
    public override long GetNeededFeatures()
    {
        return AdminFEATURE.FeedBack;
    }

    protected void InitSort()
    {
        BisSortInfo bisSortInfo = new BisSortInfo();
        DataTable dtSortList = bisSortInfo.GetAllSortList(ArticelTypeEnum.FeedBack);
        if (dtSortList != null && dtSortList.Rows.Count > 0)
        {
            int nSortIndex = 0;
            int nSortID = 0;
            foreach (DataRow dr in dtSortList.Rows)
            {
                if (Convert.ToInt32(dr["ParentID"]) == 0)
                {
                    nSortIndex = 1;
                    nSortID = 0;
                }
                else
                {
                    nSortIndex = 2;
                    nSortID = Convert.ToInt32(dr["ID"]);
                }
                ddlSort.Items.Add(new ListItem(Common.GetSortIcon(nSortIndex) + dr["Name"].ToString(), nSortID.ToString()));
            }
            dtSortList.Dispose();
        }
    }

    protected void InitData()
    {
        BisFeedBack bisFeedBack = new BisFeedBack();
        DataTable dtFaq = bisFeedBack.GetList(nID);
        if (dtFaq.Rows.Count > 0)
        {
            if (nAdminFeatures != 1 &&!string.IsNullOrEmpty(dtFaq.Rows[0]["AdminID"].ToString()))
            {
                if (Convert.ToInt32(dtFaq.Rows[0]["AdminID"])!=nAdminUserID)
                {
                    Response.Write("没有权限！");
                    Response.End();
                }
            }

            hfID.Value = dtFaq.Rows[0]["ID"].ToString();
            txtTitle.Text = dtFaq.Rows[0]["Title"].ToString();
            txtInfo.Text = dtFaq.Rows[0]["Info"].ToString();
            ddlSort.SelectedValue = dtFaq.Rows[0]["SortID"].ToString();
            txtPageTitle.Text = dtFaq.Rows[0]["PageTitle"].ToString();
            txtPageKeywords.Text = dtFaq.Rows[0]["PageKeywords"].ToString();
            txtPageDescription.Text = dtFaq.Rows[0]["PageDescription"].ToString();
            if (dtFaq.Rows.Count > 1)
            {
                hfReID.Value = dtFaq.Rows[1]["ID"].ToString();
                fckInfo.Value = dtFaq.Rows[1]["Info"].ToString();
            }
        }
    }

    /// <summary>
    /// 提交操作
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        FeedBackInfo Info = new FeedBackInfo();
        BisFeedBack bisInfo=new BisFeedBack();
        Info.ParentID = 0;
        Info.ReType = 1;
        Info.Title = txtTitle.Text.Trim();
        Info.SortID = Convert.ToInt32(ddlSort.SelectedValue);
        Info.Info = txtInfo.Text.Trim();
        Info.PageTitle = txtPageTitle.Text.Trim();
        Info.PageDescription = txtPageDescription.Text.Trim();
        Info.PageKeywords = txtPageKeywords.Text.Trim();
        Info.AdminID = nAdminUserID;
        if (strType == "add")
        {
            int nFeedBackID = bisInfo.InsertFeedBack(Info);
            if (nFeedBackID > 0)
            {
                Info.ParentID = nFeedBackID;
                Info.Info = fckInfo.Value;
                Info.SortID = null;
                Info.Title = null;
                Info.PageTitle = null;
                Info.PageDescription = null;
                Info.PageKeywords = null;
                bisInfo.Insert(Info);
                bisInfo.SetRe(nFeedBackID);
                Until.AlertJS("提交成功！", "ResetPage();");
            }
            else
            {
                Until.Alert("提交失败！");
            }
        }
        else
        {
            Info.ID = nID;
            bool bResult = bisInfo.Update(Info);
            if (bResult)
            {
                Info.ParentID = nID;
                Info.Info = fckInfo.Value;
                Info.SortID = null;
                Info.Title = null;
                Info.PageTitle = null;
                Info.PageDescription = null;
                Info.PageKeywords = null;
                if (!string.IsNullOrEmpty(hfReID.Value))
                {
                    Info.ID = Convert.ToInt32(hfReID.Value);
                    bisInfo.Update(Info);
                }
                else
                {
                    bisInfo.Insert(Info);
                }
                bisInfo.SetRe(nID);
                Until.AlertJS("提交成功！", "ResetPage();");
            }
            else
            {
                Until.Alert("提交失败！");
            }
        }
    }
}